package org.vectrics.company.web.struts;

import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.vectrics.company.Company;
import org.vectrics.company.CompanyPerson;
import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.struts.Request;
import org.vectrics.user.Person;


public class ShowCompanyPersonPrivilegeAction extends RequestFormAction {
    private transient final Logger log = Logger.getLogger(this.getClass());
	
	protected String fillForm(ActionForm formIn, Request request, HttpServletResponse response) {
		log.debug("fillForm() method - put correct values in the Struts ActionForm");
		String mapCode = "success";
		CompanyPersonPrivilegeForm form = (CompanyPersonPrivilegeForm)formIn;
		String companyIdParam = request.getParameter("companyId");
		String personIdParam = request.getParameter("personId");
		
		CompanyPerson companyPerson = null;
		Company company = CoreServiceLocator.getCompanyService().findCompany(new Long(companyIdParam));
		Person person = CoreServiceLocator.getPersonService().findPersonById(new Long(personIdParam));
		companyPerson = CoreServiceLocator.getCompanyService().findCompanyPerson(company, person);
		
		request.setAttribute("companyPersonId", companyPerson.getId().toString());
		log.info("companyPersonId = " + companyPerson.getId().toString());
		
		if (companyPerson != null) {
			form.setPersonName(companyPerson.getPerson().getName());
			form.setCompanyName(companyPerson.getCompany().getName());
		}
		return(mapCode);
	}

	protected ActionForm createForm() {
		ActionForm form = new CompanyPersonRoleForm();
		return(form);
	}
	
}
